<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: update_qqconnect.php 23052 2011-06-15 08:19:04Z monkey $
 */

include_once('../source/class/class_core.php');
include_once('../source/function/function_core.php');

@set_time_limit(0);

$cachelist = array();
$discuz = & discuz_core::instance();

$discuz->cachelist = $cachelist;
$discuz->init_cron = false;
$discuz->init_setting = false;
$discuz->init_user = false;
$discuz->init_session = false;
$discuz->init_misc = false;

$discuz->init();

$config = $_G['config']['db'];

$config['1']['dbhost'] = 'localhost';
$config['1']['dbuser'] = 'root';
$config['1']['dbpw'] = 'root';
$config['1']['dbcharset'] = 'gbk';
$config['1']['pconnect'] = '0';
$config['1']['dbname'] = 'discuz';
$config['1']['tablepre'] = 'cdb_';


$theurl = 'update_qqconnect.php';

$limit = 1000;
$start = !empty($_GET['start']) ? $_GET['start'] : 0;
if(empty($_GET['step'])) $_GET['step'] = 'start';

if($_GET['step'] == 'start') {
	show_msg('<strong>说明</strong><br />本升级程序会将Discuz! 7.2 或 Discuz! X1.5 的 QQ互联用户数据升级转换到 Discuz! X1.5.1 的 QQ互联插件。<br />请确保您已经升级到 Discuz! X1.5.1 正式版。<br /><br />
		<a href="'.$theurl.'?step=convert&from=DZ72">从 Discuz! 7.2 升级到 Discuz! X1.5.1，点击这里开始转换数据。</a> [<a href="http://www.discuz.net/thread-2179010-1-1.html" target="_blank">查看升级教程</a>]<br /><br />
		<a href="'.$theurl.'?step=convert&from=DX15">从 Discuz! X1.5 升级到 Discuz! X1.5.1，点击这里开始转换数据。</a> [<a href="http://www.discuz.net/thread-2176266-1-1.html" target="_blank">查看升级教程</a>]
	');

} elseif($_GET['step'] == 'convert') {

	if($_GET['from'] == 'DX15') {

		$query = DB::query("SELECT COUNT(*) FROM ".DB::table('common_member')." WHERE conuin<>''", 'SILENT');
		$count = $query ? DB::result($query, 0) : 0;

		if(empty($count)) {
			show_msg("很抱歉，当前站点没有可升级转换的数据。<br /><br /><br />提示：请及时删除此程序，消除隐患。");
		}

		if($start <= $count) {
			$query = DB::query("SELECT * FROM ".DB::table('common_member')." WHERE conuin<>'' ORDER BY uid LIMIT $start, $limit");
			while($row = DB::fetch($query)) {
				$row = daddslashes($row);
				$qc_user = DB::fetch_first("SELECT * FROM ".DB::table('common_member_connect')." WHERE uid='$row[uid]'");
				if(!$qc_user) {
					$data = array(
						'uid' => $row['uid'],
						'conuin' => $row['conuin'],
						'conispublishfeed' => $row['conispublishfeed'],
						'conispublisht' => $row['conispublisht'],
						'conisregister' => $row['conisregister'],
						'conisqzoneavatar' => $row['conisqzoneavatar'],
						'conisfeed' => 1,
					);
					DB::insert('common_member_connect', $data, false, true);
				}
			}
			$start += $limit;
			show_msg("QQ 互联用户数据升级中 ... $start/$count", "$theurl?step=convert&from=DX15&start=$start");
		} else {
			show_msg("恭喜，QQ 互联用户数据升级完毕。<br /><br /><br />提示：请及时删除此程序，消除隐患。");
		}

	} else {

		$dbold = new db_mysql($config);
		$dbold->connect(1);

		$copys = array(
			'from' => array('conmemberbindlog', 'uinblack', 'confeedlog', 'contlog'),
			'to' => array('connect_memberbindlog', 'common_uin_black', 'connect_feedlog', 'connect_tlog'),
		);

		$table = !empty($_GET['table']) ? $_GET['table'] : 0;
		if($table !== 'member') {
			$oldquery = $dbold->query("SELECT COUNT(*) FROM ".$dbold->table_name($copys['from'][$table]), 'SILENT');
			$count = $oldquery ? $dbold->result($oldquery, 0) : 0;

			if($start <= $count) {
				$oldquery = $dbold->query("SELECT * FROM ".$dbold->table_name($copys['from'][$table])." LIMIT $start, $limit");
				while($row = $dbold->fetch_array($oldquery)) {
					$row = daddslashes($row);
					$is_exist = DB::fetch_first("SELECT * FROM ".DB::table($copys['to'][$table])." WHERE uid='$row[uid]' AND dateline='$row[dateline]'");
					if(!$is_exist) {
						DB::insert($copys['to'][$table], $row, false, true);
					}
				}
				$start += $limit;
				show_msg("QQ 互联用户数据升级中 ... ".$copys['from'][$table].": $start/$count...", "$theurl?step=convert&from=DZ72&table=$table&start=$start");
			} else {
				$table++;
				if($copys['from'][$table]) {
					show_msg("QQ 互联用户数据升级中 ... ".$copys['from'][$table].": 完成...", "$theurl?step=convert&from=DZ72&table=$table&start=0");
				} else {
					show_msg("QQ 互联用户数据升级中 ... members", "$theurl?step=convert&from=DZ72&table=member&start=0");
				}
			}
		} else {

			$query = $dbold->query("SELECT COUNT(*) FROM ".$dbold->table_name('members')." WHERE conuin<>''", 'SILENT');
			$count = $query ? DB::result($query, 0) : 0;

			if($start <= $count) {
				$query = $dbold->query("SELECT * FROM ".$dbold->table_name('members')." WHERE conuin<>'' ORDER BY uid LIMIT $start, $limit");
				while($row = $dbold->fetch_array($query)) {
					$row = daddslashes($row);
					$qc_user = DB::fetch_first("SELECT * FROM ".DB::table('common_member_connect')." WHERE uid='$row[uid]'");
					if(!$qc_user) {
						$data = array(
							'uid' => $row['uid'],
							'conuin' => $row['conuin'],
							'conispublishfeed' => $row['conispublishfeed'],
							'conispublisht' => $row['conispublisht'],
							'conisregister' => $row['conisregister'],
							'conisqzoneavatar' => $row['conisqzoneavatar'],
							'conisfeed' => 1,
						);
						DB::insert('common_member_connect', $data, false, true);
						DB::update('common_member', array('conisbind' => $row['conisbind']), "uid='$row[uid]'");
					}
				}
				$start += $limit;
				show_msg("QQ 互联用户数据升级中 ... members: $start/$count", "$theurl?step=convert&from=DZ72&table=member&start=$start");
			} else {
				$query = $dbold->query("SELECT variable, value FROM ".$dbold->table_name('settings')." WHERE variable IN ('connectsiteid', 'connectsitekey')");
				while($row = $dbold->fetch_array($query)) {
					$settings_old[$row['variable']] = $row['value'];
				}
				$query = DB::query("SELECT skey, svalue FROM ".DB::table('common_setting')." WHERE skey IN ('my_siteid', 'my_sitekey')");
				while($row = DB::fetch($query)) {
					$settings[$row['skey']] = $row['svalue'];
				}
				DB::insert('common_setting', array(
					'skey' => 'connectsiteid',
					'svalue' => $settings_old['connectsiteid'],
				), false, true);
				if(empty($settings['my_siteid']) && !empty($settings_old['connectsiteid'])) {
					DB::insert('common_setting', array(
						'skey' => 'my_siteid',
						'svalue' => $settings_old['connectsiteid'],
					), false, true);
					DB::delete('common_setting', "skey='connectsiteid'");
				}
				DB::insert('common_setting', array(
					'skey' => 'connectsitekey',
					'svalue' => $settings_old['connectsitekey'],
				), false, true);
				if(empty($settings['my_sitekey']) && !empty($settings_old['connectsitekey'])) {
					DB::insert('common_setting', array(
						'skey' => 'my_sitekey',
						'svalue' => $settings_old['connectsitekey'],
					), false, true);
					DB::delete('common_setting', "skey='connectsitekey'");
				}
				show_msg("恭喜，QQ 互联用户数据升级完毕。<br /><br /><br />提示：请及时删除此程序，消除隐患。");
			}
		}

	}

}

function show_msg($message, $url_forward='') {

	if($url_forward) {
		$message = "<a href=\"$url_forward\">$message (跳转中...)</a><script>setTimeout(\"window.location.href ='$url_forward';\", 1);</script>";
	}

	show_header();
	print<<<END
	<table>
	<tr><td>$message</td></tr>
	</table>
END;
	show_footer();
	exit();
}


function show_header() {
	global $config;

	$nowarr = array($_GET['step'] => ' class="current"');

	print<<<END
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
	<title> QQ 互联数据库升级工具 </title>
	<style type="text/css">
	* {font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 1.5em; word-break: break-all; }
	body { text-align:center; margin: 0; padding: 0; background: #F5FBFF; }
	.bodydiv { margin: 40px auto 0; width:720px; text-align:left; border: solid #86B9D6; border-width: 5px 1px 1px; background: #FFF; }
	h1 { font-size: 18px; margin: 1px 0 0; line-height: 50px; height: 50px; background: #E8F7FC; color: #5086A5; padding-left: 10px; }
	#menu {width: 100%; margin: 10px auto; text-align: center; }
	#menu td { height: 30px; line-height: 30px; color: #999; border-bottom: 3px solid #EEE; }
	.current { font-weight: bold; color: #090 !important; border-bottom-color: #F90 !important; }
	input { border: 1px solid #B2C9D3; padding: 5px; background: #F5FCFF; }
	#footer { font-size: 10px; line-height: 40px; background: #E8F7FC; text-align: center; height: 38px; overflow: hidden; color: #5086A5; margin-top: 20px; }
	</style>
	</head>
	<body>
	<div class="bodydiv">
	<h1>QQ 互联数据库升级工具</h1>
	<div style="width:90%;margin:0 auto;">
	<br>
END;
}

function show_footer() {
	print<<<END
	</div>
	<div id="footer">&copy; Comsenz Inc. 2001-2011 http://www.comsenz.com</div>
	</div>
	<br>
	</body>
	</html>
END;
}

?>